rm(list=ls())  
if(Sys.info()['user']=='dannychoi'){pathData<-"/Users/dannychoi/Dropbox/Projects/DCMPNS_Language/DataAnalysis"}
setwd(pathData)
library(stargazer)
library(estimatr)
library(readr)
library(lubridate)
library(tidyverse)
library(Rmisc)
library(lfe)



#############################################
#### Choi, Poertner, and Sambanis - JEPS ####
#############################################

##### Replication File Part I. Data Cleaning

# Importing the raw experimental data for two experiments
datafirst  <- read.csv(file="data1.csv", stringsAsFactors = F)
datasecond <- read.csv(file="data2.csv", stringsAsFactors = F)

##### Creating Treatment Variables 

##  Experiment 1

## Main Figure 3: Discrimination Plot Comparisons
datafirst$language <- NA
datafirst$language[datafirst$trtype==7 | datafirst$trtype==10] <- 1 # Hijab, German/ Foreign Language  (Column 1)
datafirst$language[datafirst$trtype==9 | datafirst$trtype==12]  <- 2 # No Hijab, German/ Foreign Language  (Column 2)
datafirst$language[datafirst$trtype==14] <- 3 # Native (Column 3)

# No hijab vs native comparison
datafirst$nhvn <- NA
datafirst$nhvn[datafirst$language==2] <- 1
datafirst$nhvn[datafirst$language==3] <- 0

# Hijab vs native comparison
datafirst$hvn <- NA
datafirst$hvn[datafirst$language==1] <- 1
datafirst$hvn[datafirst$language==3] <- 0

# Hijab vs no hijab comparison
datafirst$hvnh <- NA
datafirst$hvnh[datafirst$language==1] <- 1
datafirst$hvnh[datafirst$language==2] <- 0



## Main Figure 4: Language Effects Plot Comparisons
datafirst$languageoff <- NA
datafirst$languageoff[datafirst$trtype==7]   <- 1 # hijab foreign 
datafirst$languageoff[datafirst$trtype==10]  <- 2 # hijab German
datafirst$languageoff[datafirst$trtype==9]   <- 3 # no hijab foreign
datafirst$languageoff[datafirst$trtype==12]  <- 4 # no hijab German
datafirst$languageoff[datafirst$trtype==14]  <- 5 # native

# Hijab foreign Language vs Hijab German language
datafirst$hfvhg <- NA
datafirst$hfvhg[datafirst$trtype==7]  <- 1
datafirst$hfvhg[datafirst$trtype==10] <- 0

# No hijab foreign language vs No hijab German language
datafirst$nfvng <- NA
datafirst$nfvng[datafirst$trtype==9]  <- 1
datafirst$nfvng[datafirst$trtype==12] <- 0

# Hijab German language vs Native 
datafirst$hgvn <- NA
datafirst$hgvn[datafirst$trtype==10] <- 1 
datafirst$hgvn[datafirst$trtype==14]  <- 0 

# Hijab German language vs No hijab German language
datafirst$hgvnhg <- NA
datafirst$hgvnhg[datafirst$trtype==10] <- 1
datafirst$hgvnhg[datafirst$trtype==12] <- 0


## Experiment 2

datasecond$language[datasecond$trtype==10 | datasecond$trtype==3] <- 1 # Hijab, German/Foreign Language  (Column 1)
datasecond$language[datasecond$trtype==11 | datasecond$trtype==6 ] <- 2 # No Hijab, German/Foreign Language  (Column 2)
datasecond$language[datasecond$trtype==9] <- 3 # Native (Column 3)

# No hijab vs native comparison
datasecond$nhvn[datasecond$language==2] <- 1
datasecond$nhvn[datasecond$language==3] <- 0

# Hijab vs native comparison
datasecond$hvn[datasecond$language==1] <- 1
datasecond$hvn[datasecond$language==3] <- 0

# Hijab vs no hijab comparison
datasecond$hvnh <- NA
datasecond$hvnh[datasecond$language==1] <- 1
datasecond$hvnh[datasecond$language==2]  <- 0


## Main Figure 4: Language Effects Plot Comparisons
datasecond$languageoff[datasecond$trtype==10] <- 1 # hijab foreign
datasecond$languageoff[datasecond$trtype==3]  <- 2 # hijab german
datasecond$languageoff[datasecond$trtype==11] <- 3 # no hijab foreign
datasecond$languageoff[datasecond$trtype==6]  <- 4 # no hijab german                    
datasecond$languageoff[datasecond$trtype==9]  <- 5 # native german

# Hijab foreign language vs Hijab German language
datasecond$hfvhg <- NA
datasecond$hfvhg[datasecond$trtype==10] <- 1
datasecond$hfvhg[datasecond$trtype==3]  <- 0
  
# Hijab vs native comparison
datasecond$nfvng <- NA
datasecond$nfvng[datasecond$trtype==11] <- 1
datasecond$nfvng[datasecond$trtype==6]  <- 0

# Hijab German language vs Native 
datasecond$hgvn <- NA
datasecond$hgvn[datasecond$trtype==3] <- 1
datasecond$hgvn[datasecond$trtype==9] <- 0

# Hijab German language vs No hijab German language
datasecond$hgvnhg <- NA
datasecond$hgvnhg[datasecond$trtype==3] <- 1
datasecond$hgvnhg[datasecond$trtype==6] <- 0


##### Merging data from both experiments
colnames(datasecond)[colnames(datasecond)=="anyhelp"] <- "help"

# Coding which experiment
datafirst$exp  <- 1
datasecond$exp <- 2

# Merging the two experimental data sets
merged <- rbind(datafirst, datasecond)

# Drop observations that are not in any of the treatment conditions

merged <- merged %>%
  filter(!is.na(languageoff))


# Exporting merged data set to RData file.

save(merged, file="merged.RData")

